package com.xianyun.book.gatewayimpl.handle.strategy;

import com.xianyun.book.common.core.handle.SingleHandle;
import com.xianyun.book.common.core.handle.SingleInvoker;
import com.xianyun.book.common.core.handle.HandleParam;
import com.xianyun.book.convertor.BookConvertor;
import com.xianyun.book.domain.book.Book;
import com.xianyun.book.gatewayimpl.database.BookMapper;
import com.xianyun.book.gatewayimpl.database.dataobject.BookDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;

/**
 * @author macos-zyj
 */
@Slf4j
@Component
public class BookDatabaseSingleHandle implements SingleHandle<Book,String> {

    @Resource
    private BookMapper bookMapper;


    @Override
    public Book doQuery(HandleParam<String> handleParam, SingleInvoker<Book, String> next) {
        String isbn = handleParam.getHandleData();
        BookDO result = bookMapper.selectBookByIsbn(isbn);
        if (null !=  result){
            return BookConvertor.toDomainObject(result);
        } else {
            return next.invoke(handleParam);
        }
    }
}
